package bbsimon.first.model.reg;

import java.io.*;
import java.util.regex.*;
import java.util.*;

public class RegModel {
	private boolean checkEmail(String email) {
		return Pattern.matches("^[a-zA-Z]\\w*@\\w+\\.\\w{2,4}$", email);
	}
	private boolean checkPassword(String password) {
		return Pattern.matches("^\\w{6,12}$", password);
	}

	public void reg(String email, String password) throws Exception {
		if(checkEmail(email)) {
			if(checkPassword(password)) {
				File f = new File("regs");
				BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
				String str = email + "|" + password;
				bw.write(str);
				bw.newLine();
				bw.close();
			}
			else throw new Exception("fail password checking");
		}
		else throw new Exception("fail email checking");
	}

	public List<String> list() throws IOException {
		File f = new File("regs");
		BufferedReader br = new BufferedReader(new FileReader(f));
		String line = null;
		List<String> all = new ArrayList<String>();
		while((line = br.readLine()) != null) {
			all.add(line);
		}
		return all;
	}
}
